package com.sxg.controller;

import com.sxg.convert.SxgStockConvert;
import com.sxg.entity.SxgStockEntity;
import com.sxg.query.SxgStockQuery;
import com.sxg.vo.SxgStockVO;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgStockService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 商品库存
*
* @author 浪漫写尽歌谱 3298751420@qq.com
* @since 1.0.0 2024-03-07
*/
@RestController
@RequestMapping("sxg/sxg_stock")
@AllArgsConstructor
public class SxgStockController {
    private final SxgStockService sxgStockService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sxg:sxg_stock:page')")
    public Result<PageResult<SxgStockVO>> page(@Valid SxgStockQuery query){
        PageResult<SxgStockVO> page = sxgStockService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_stock:info')")
    public Result<SxgStockVO> get(@PathVariable("id") Long id){
        SxgStockEntity entity = sxgStockService.getById(id);

        return Result.ok(SxgStockConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sxg:sxg_stock:save')")
    public Result<String> save(@RequestBody SxgStockVO vo){
        sxgStockService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_stock:update')")
    public Result<String> update(@RequestBody @Valid SxgStockVO vo){
        sxgStockService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sxg:sxg_stock:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sxgStockService.delete(idList);

        return Result.ok();
    }
}